package com.kun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.dto.DataSourceDTO;
import com.kun.entity.KunDatasource;
import com.kun.enums.DsTypeEnums;
import com.kun.service.KunDatasourceService;
import com.kun.util.ResponseResult;
import com.kun.vo.DsFileVO;
import com.kun.vo.DsUploadVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 数据源管理
 *
 * @author kun.li
 */
@RestController
@RequestMapping("ds")
@RequiredArgsConstructor
@Tag(name = "数据源管理")
public class DataSourceController {

    private final KunDatasourceService kunDatasourceService;

    @Operation(summary = "获取数据来源表格")
    @PostMapping("pageTableData")
    public ResponseResult<Page<KunDatasource>> pageTableData(@RequestBody DataSourceDTO dataSourceDTO) {
        QueryWrapper<KunDatasource> queryWrapper = new QueryWrapper<>();
        Page<KunDatasource> kunDatasourcePage =
                kunDatasourceService.page(Page.of(dataSourceDTO.getPage(), dataSourceDTO.getSize()), queryWrapper);
        return ResponseResult.success(kunDatasourcePage);
    }


    @Operation(summary = "保存数据源")
    @PostMapping("saveDs")
    public ResponseResult<Boolean> saveDs(@RequestBody KunDatasource kunDatasource) {
        return ResponseResult.success(kunDatasourceService.saveDs(kunDatasource));
    }

    @Operation(summary = "编辑数据源")
    @PostMapping("updateDs")
    public ResponseResult<Boolean> updateDs(@RequestBody KunDatasource kunDatasource) {
        return ResponseResult.success(kunDatasourceService.updateDs(kunDatasource));
    }

    @Operation(summary = "批量删除数据源")
    @PostMapping("batchDeleteDs")
    public ResponseResult<Boolean> batchDeleteDs(@RequestBody List<String> dsIds) {
        return ResponseResult.success(kunDatasourceService.removeBatchByIds(dsIds));
    }

    @Operation(summary = "校验数据源连接")
    @PostMapping("checkDsConnection")
    public ResponseResult<Boolean> checkDsConnection(@RequestBody KunDatasource kunDatasource) {
        Boolean hasConnectionSuccess = kunDatasourceService.checkDsConnection(kunDatasource);
        return ResponseResult.success(hasConnectionSuccess);
    }

    @Operation(summary = "上传驱动")
    @PostMapping("uploadDriver")
    public ResponseResult<DsFileVO> uploadDriver(String dsType, MultipartFile file) {
        return ResponseResult.success(kunDatasourceService.uploadDriver(dsType, file));
    }

    @Operation(summary = "下载驱动")
    @GetMapping("downDriver/{dsType}")
    public ResponseResult<DsFileVO> downDriver(@PathVariable String dsType) {
        return ResponseResult.success(kunDatasourceService.downDriver(dsType));
    }

    @Operation(summary = "获取所有支持的数据源")
    @GetMapping("dsTypes")
    public ResponseResult<List<String>> dsTypes() {
        return ResponseResult.success(DsTypeEnums.dsTypeList());
    }

    @Operation(summary = "查询所有jar")
    @GetMapping("queryJarList/{dsType}")
    public ResponseResult<List<DsUploadVO>> queryJarList(@PathVariable String dsType) {
        return ResponseResult.success(kunDatasourceService.queryJar(dsType));
    }

}
